"""
迭代器：实现了迭代器协议的对象。
Python中通过魔术方法来表示协议，迭代器协议对应两个魔术方法：
~ __iter__：获取迭代器对象
~ __next__：获取下一个元素
"""


class FibIter:

    def __init__(self, num):
        self.a, self.b = 0, 1
        self.counter = 0
        self.num = num

    def __iter__(self):
        return self

    def __next__(self):
        if self.counter < self.num:
            self.a, self.b = self.b, self.a + self.b
            self.counter += 1
            return self.a
        raise StopIteration()


fib_iter = FibIter(20)
for value in fib_iter:
    print(value)
